package com.ys.emp.action;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ys.emp.service.EmpService;
import com.ys.pojo.TblEmp;
import com.ys.pojo.TblMenu;

@Controller
@RequestMapping("/emp")
public class EmpAction {
	@Autowired
	private EmpService empService;
	
	//登陆
	@RequestMapping("/login")
	public String login(@Validated TblEmp emp,BindingResult bindingResult,Model model,HttpServletRequest request){
		List<ObjectError> allErrors = bindingResult.getAllErrors();
		if(allErrors.size()>0){
			model.addAttribute("error", allErrors);//带回错误的信息
			return "/login.jsp";
		}
		Map<String, Object> map = empService.getEmpByName(emp);
		if(map.size()==2){
			//登陆成功
			TblEmp e = (TblEmp) map.get("emp");
			HttpSession session = request.getSession();
			session.setAttribute("emp", e);
			return "/index.jsp";
		}else{
			model.addAttribute("msg", map.get("msg"));
			return "/login.jsp";
		}
	}
	
	
	//登陆成功后给员工分配指定权限
	@RequestMapping("/getTblMenu")
	@ResponseBody
	public List<TblMenu> getTblMenu(Integer uuid){
		return empService.getTblMenu(uuid);
	}
	
	//退出
	@RequestMapping("/zhuXiao")
	public String zhuXiao(HttpServletRequest request){
		request.getSession().invalidate();
		return "/login.jsp";
	}
}
